/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;
import controllers.entity.ChamThi;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author hoangnv
 */
public class ChamThiDAO {
    private static PreparedStatement prst = null;
    public static List <ChamThi>  getAllChamThiByMaCanBo(int MaCanBo){
        List <ChamThi> list = new ArrayList<>();
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return null;
        }
        try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("SELECT * FROM ChamThi WHERE MaCanBo = ?");
            prst.setInt(1, MaCanBo);
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                
                int maCanBo = rs.getInt("MaCanBo");
                String maLopHoc = rs.getString("MaLopHoc");
                String maMonHoc = rs.getString("MaMonHoc");
                Date ngayNop = rs.getDate("NgayNop");
                int soBaiThi = rs.getInt("SoBaiThi");
                ChamThi temp = new ChamThi(maLopHoc,maCanBo,maMonHoc,soBaiThi,ngayNop);
                list.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        DBConnect.Close();
        return list;
    }
     public static List <ChamThi>  getAllChamThi(){
        List <ChamThi> list = new ArrayList<>();
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return null;
        }
        try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("SELECT * FROM ChamThi");
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                
                int maCanBo = rs.getInt("MaCanBo");
                String maLopHoc = rs.getString("MaLopHoc");
                String maMonHoc = rs.getString("MaMonHoc");
                Date ngayNop = rs.getDate("NgayNop");
                int soBaiThi = rs.getInt("SoBaiThi");
                ChamThi temp = new ChamThi(maLopHoc,maCanBo,maMonHoc,soBaiThi,ngayNop);
                list.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        DBConnect.Close();
        return list;
    }
    public boolean InsertChamThi(ChamThi X){
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return false;
        }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("INSERT INTO ChamThi"
                    + "(MaLopHoc,MaCanBo,MaMonHoc,SoBaiThi,NgayNop)"
                    + "VALUES (?,?,?,?,?)");
            prst.setString(1, X.getMaLopHoc());
            prst.setInt(2,X.getMaCanBo());
            prst.setString(3,X.getMaMonHoc());
            prst.setInt(4,X.getSoBaiThi());
            prst.setDate(5,new java.sql.Date((X.getNgayNop()).getTime()));
            if (prst.executeUpdate() > 0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
    public boolean UpdateChamThi(ChamThi X){
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return false;
        }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("UPDATE ChamThi SET MaCanBo = ?, "
                    + "MaMonHoc = ?, NgayNop = ?, SoBaiThi = ? WHERE MaLopHoc = ?");
            prst.setInt(1,X.getMaCanBo());
            prst.setString(2,X.getMaMonHoc());
            prst.setDate(3,new java.sql.Date((X.getNgayNop()).getTime()));
            prst.setInt(4,X.getSoBaiThi());
            prst.setString(5,X.getMaLopHoc()); 
            if (prst.executeUpdate() >0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
    public boolean DeleteChamThiByMaLopHoc(String MaLopHoc){
       boolean ok = DBConnect.Connect();
       if (!ok) {
            DBConnect.Close();
            return false;
       }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("DELETE FROM ChamThi WHERE MaLopHoc = ? ");
            prst.setString(1,MaLopHoc);
            if (prst.executeUpdate() >0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
}
